import { h } from "koishi"
import sept from "@lnnbot-private/7.json"
import strategy from "@lnnbot-private/sept-stat-poss-6.json"
import { translate } from "@lnnbot-private/koishi-session-locales"

export const name = "md5-compete"

export function apply(ctx) {

  ctx.command("md5-compete <name1> <name2>")
  .option("dice", "-d")
  .action( ({session, options}, name1, name2 ) => {
    var salt = "成都A 绵阳B　C产盐巴"
    let p1md5 = require("crypto").hash("md5", salt + name1 + name2)
    let p2md5 = require("crypto").hash("md5", salt + name2 + name1)
    var stra_poss_1 = Array.from( {length:6}, (_, i) => {
      return parseInt( p1md5.substring( i*5, i*5+5 ) , 16 )
    } )
    var stra_poss_2 = Array.from( {length:6}, (_, i) => {
      return parseInt( p2md5.substring( i*5, i*5+5 ) , 16 )
    } )
    var remain1 = (1 << 7) - 1
    var remain2 = (1 << 7) - 1
    var score = [0, 0]
    var outp = [session.text(".start", [name1, name2]), h("br"), h("br")]
    for (let round = 0; round < 6; round++) {
      let remain12 = remain1 | (remain2 << 7)
      let remain21 = remain2 | (remain1 << 7)
      let choice1 = strategy[remain12].findIndex(
        (v,i) => stra_poss_1[round] >= v && stra_poss_1[round] < strategy[remain12][i + 1]
      )
      let choice2 = strategy[remain21].findIndex(
        (v,i) => stra_poss_2[round] >= v && stra_poss_2[round] < strategy[remain21][i + 1]
      )
      if (options.dice) {
        let face1 = sept[choice1].t6j[Math.floor(Math.random() * 3)]
        let face2 = sept[choice2].t6j[Math.floor(Math.random() * 3)]
        let win = face1 > face2 ? 1 : face1 < face2 ? -1 : 0
      ctx.logger.debug({ round, remain1, remain2, remain12, remain21,  choice1, choice2 })
        outp.push(
          session.text(`.rounds.${round}`),
          session.text(".fight-dice", [
            name1,
            name2,
            translate(sept[choice1].suemi8H, session),
            translate(sept[choice2].suemi8H, session),
            face1,
            face2
          ])
        )
        if (win > 0) {
          score[0]++
          outp.push(
            session.text(".victor", [name1, score[0], score[1]])
          )
        }
        else if (win < 0) {
          score[1]++
          outp.push(
            session.text(".victor", [name2, score[0], score[1]])
          )
        }
        else {
          outp.push(
            session.text(".tie", [score[0], score[1]])
          )
        }
      }
      else {
        let win = sept[choice1].ke[choice2].gElia
        outp.push(
          session.text(`.rounds.${round}`),
          session.text(".fight", [
            name1,
            name2,
            sept[choice1].fEE,
            sept[choice2].fEE
          ])
        )
        if (win > 0) {
          score[0]++
          outp.push(
            session.text(".element-victor", [
              sept[choice1].fEE,
              sept[choice2].fEE,
              sept[choice1].ke[choice2].tYiHo[0] //如果有下标大于0的变体台词，可以在此修改代码触发特效
            ]),
            session.text(".victor", [name1, score[0], score[1]]),
          )
        }
        else if (win < 0) {
          score[1]++
          outp.push(
            session.text(".element-victor", [
              sept[choice2].fEE,
              sept[choice1].fEE,
              sept[choice2].ke[choice1].tYiHo[0] //如果有下标大于0的变体台词，可以在此修改代码触发特效
            ]),
            session.text(".victor", [name2, score[0], score[1]]),
          )
        }
        else {
          outp.push(
            session.text(".tie", [score[0], score[1]])
          )
        }
      }
      outp.push(h("br"), h("br"))
      if (score[0] >= 4 || score[1] >= 4) break
      remain1 = remain1 & ~(1 << choice1)
      remain2 = remain2 & ~(1 << choice2)
    }
    if (score[0] > score[1])
      outp.push(session.text(".final-victor", [name1, name2, score[0], score[1]]))
    else if (score[1] > score[0])
      outp.push(session.text(".final-victor", [name2, name1, score[1], score[0]]))
    else
      outp.push(session.text(".final-tie", [name1, name2, score[0], score[1]]))
    return outp.join("")
  } )
  ctx.i18n.define("zh-CN", "commands.md5-compete", {
    description: "利用MD5码进行名字大作战，以七元素猜拳的形式",
    arguments: { "name1": "参与者1" , "name2": "参与者2" },
    options: { "dice": "使用骰子作战，增加随机性" },
    messages: {
      "start": "【{0}】与【{1}】的战斗现在开始！",
      "rounds": [ "第一轮，" , "第二轮，" , "第三轮，" , "第四轮，" , "第五轮，" , "第六轮，" , "第七轮，" ],
      "fight": "玩家【{0}】使用【{2}】元素，玩家【{1}】使用【{3}】元素，",
      "fight-dice": "玩家【{0}】用{2}色骰子掷出 {4} 点，玩家【{1}】用{3}色骰子掷出 {5} 点，",
      "element-victor": "触发元素相克技能之【{0}克{1}：{2}】，",
      "victor": "玩家【{0}】拿下本轮胜利，当前比分为 {1}:{2}。",
      "tie": "本轮平局，当前比分仍为 {0}:{1}。",
      "final-victor": "恭喜玩家【{0}】以 {2} 比 {3} 的优势战胜玩家【{1}】！",
      "final-tie": "恭喜玩家【{0}】和玩家【{1}】在 {2}:{3} 的比分下握手言和！",
    }
  })
  ctx.i18n.define("en-US", "commands.md5-compete", {
    description: "Fight by MD5 of names via Sept-elemental finger-guessing",
    arguments: { "name1": "Player1" , "name2": "Player2" },
    options: { "dice": "Use dice and make the game more random" },
    messages: {
      "start": "[{0}] vs [{1}] Fight!",
      "rounds": [ "Round 1:" , "Round 2:" , "Round 3:" , "Round 4:" , "Round 5:" , "Round 6:" , "Round 7:" ],
      "fight": "[{0}] throws {2} and [{1}] throws {3}. ",
      "fight-dice": "[{0}] rolls the {2} die and gets a {4}, whil [{1}] rolls the {3} die and gets a {5}. ",
      "element-victor": "",
      "victor": "[{0}] wins this round, and the current score is {1}:{2}.",
      "tie": "It makes a tie, and the current score is still {0}:{1}.",
      "final-victor": "[{0}] defeats [{1}] with a score of {2} to {3}. Congratulations!",
      "final-tie": "[{0}] and [{1}] draw the game in a {2}:{3} tie. Congratulations!",
    }
  })
  ctx.i18n.define("mozeinn", "commands.md5-compete", {
    description: "奶奶们，用MD5码打一架！七元素の猜拳に！",
    arguments: { "name1": "奶奶甲" , "name2": "奶奶乙" },
    options: { "dice": "一满搞骰子也" },
    messages: {
      "start": "快来看【{0}】和【{1}】打架啦！",
      "rounds": [ "开战！" , "二战！" , "三战！" , "四战！" , "五战！" , "终焉！" , "还打？" ],
      "fight": "【{0}】跟我一起画个{2}，在你【{1}】画一道{3}，",
      "fight-dice": "【{0}】的{2}色骰子搞出了 {4} 点，【{1}】的{3}色骰子搞出了 {5} 点，",
      "element-victor": "{0}玩意儿把{1}玩意儿杀得那叫一个“{2}”哩！",
      "victor": "【{0}】赢了！比分 {1}:{2} ",
      "tie": "不好意思，两败俱伤了啊啊。比分仍为 {0}:{1}。",
      "final-victor": "结论：【{0}】赢麻了！【{1}】输惨了！",
      "final-tie": "结论：【{0}】和【{1}】不相上下！",
    }
  })

}
